<% layout('/layouts/default.html', {title: '代理管理', libs: ['dataGrid']}){ %>
<style>
	.ui-jqgrid .actions>a i {
		font-size: 20px;
	}
</style>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header" style="display: none;">
			<div class="box-title">
				<i class="fa icon-user"></i> ${text('用户管理')}
			</div>
			<div class="box-tools pull-right">
				<a href="#" class="btn btn-default" id="btnSearch" title="${text('查询')}"><i class="fa fa-filter"></i> ${text('查询')}</a>
				<% if(hasPermi('vip:agent:edit')){ %>
				<a href="${ctx}/vip/agent/form?op=add" class="btn btn-default btnTool" title="${text('新增用户')}"><i class="fa fa-plus"></i> ${text('新增')}</a>
				<% } %>
				<div class="btn-group">
					<a href="javascript:" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
						<i class="fa fa-navicon"></i> <span class="caret"></span>
					</a>
					<ul class="dropdown-menu">
						<li><a href="javascript:" id="btnExport"><i class="glyphicon glyphicon-export"></i> 导出</a></li>
					</ul>
				</div>
			</div>
		</div>
		<div class="box-body">
			<#form:form id="searchForm" model="${empUser}" action="${ctx}/vip/agent/listData" method="post" class="form-inline "
					data-page-no="${parameter.pageNo}" data-page-size="${parameter.pageSize}" data-order-by="${parameter.orderBy}">
			</#form:form>
			<table id="dataGrid"></table>
			<div id="dataGridPage"></div>
		</div>
	</div>
</div>
<% } %>
<script>
    // 初始化DataGrid对象
    $('#dataGrid').dataGrid({
        searchForm: $("#searchForm"),
        columnModel: [
            {header:'${text('登录账号')}', name:'loginCode', index:'a.login_code', width:80, align:"center", frozen:true, formatter: function(val, obj, row, act){
                return '<a href="${ctx}/vip/member/list?agentId='+row.userCode+'" class="btnList" data-title="${text('会员列表')}">'+(val||row.id)+'</a>';
            }},
            {header:'${text('名称')}', name:'refName', index:'a.ref_name', width:80, align:"center" ,formatter: function(val, obj, row, act){
                    return val;
            }},
            {header:'${text('角色')}', name:'roleName',field:'roleName', width:70, align:"center" ,formatter: function(val, obj, row, act){
                    return val;
                }},
            {header:'${text('会员人数')}', name:'vipCount', index:'a.vipCount', width:80, align:"center" ,formatter: function(val, obj, row, act){
                    return val;
                }},
            {header:'${text('状态')}', name:'status', index:'a.status', width:70, align:"center", formatter: function(val, obj, row, act){
                    return js.getDictLabel(${@DictUtils.getDictListJson('sys_status')}, val, '未知', true);
                }},
            {header:'${text('总余额')}', name:'agentBalance', index:'a.agentBalance', width:80, align:"center", formatter: function(val, obj, row, act){
                    return formatNumber(val);
                }},
            {header:'${text('操作')}', name:'actions', width:110, sortable:false, title:false, formatter: function(val, obj, row, act){
                    if(row.extend.extendS1 == '4'){//系统管理员不提供修改
                        return [];
                    }
                    var actions = [];
                <% if(hasPermi('vip:agent:edit')){ %>
                        actions.push('<a href="${ctx}/vip/agent/form?userCode='+row.userCode+'&op=edit" class="btnList" title="${text('编辑用户')}"><i class="fa fa-pencil"></i></a>&nbsp;');
                        if (row.status == Global.STATUS_NORMAL){
                            actions.push('<a href="${ctx}/vip/agent/disable?userCode='+row.userCode+'" class="btnList" title="${text('停用用户')}" data-confirm="${text('确认要停用该用户吗？')}"><i class="glyphicon glyphicon-ban-circle"></i></a>&nbsp;');
                        }
                        if (row.status == Global.STATUS_DISABLE){
                            actions.push('<a href="${ctx}/vip/agent/enable?userCode='+row.userCode+'" class="btnList" title="${text('启用用户')}" data-confirm="${text('确认要启用该用户吗？')}"><i class="glyphicon glyphicon-ok-circle"></i></a>&nbsp;');
                        }
                    <% } %>
                <% if(hasPermi('vip:agent:edit')){ %>
                        if (row.status == Global.STATUS_DISABLE){
                            actions.push('<a href="${ctx}/vip/agent/delete?userCode='+row.userCode+'" class="btnList" title="${text('删除用户')}" data-confirm="${text('确认要删除该用户吗？')}"><i class="fa fa-trash-o"></i></a>&nbsp;');
                        }
                        actions.push('<a href="${ctx}/vip/member/form?userCode='+row.userCode+'" class="btnList" title="新增代理会员"><i class="fa fa-plus-square"></i></a>&nbsp;');
                    <% } %>
                <% if(hasPermi('vip:agent:authRole,vip:agent:authDataScope,vip:agent:resetpwd', 'or')){ %>
                        actions.push('<a href="javascript:" class="btnMore" title="${text('更多操作')}"><i class="fa fa-chevron-circle-right"></i></a>&nbsp;');
                        actions.push('<div class="moreItems">');
                    <% if(hasPermi('vip:agent:authRole')){ %>
                            actions.push('<a href="${ctx}/vip/agent/form?userCode='+row.userCode+'&op=auth" class="btn btn-default btn-xs btnList" title="${text('用户分配角色')}"><i class="fa fa-check-square-o"></i> ${text('分配角色')}</a>&nbsp;');
                        <% } %>
                    <% if(hasPermi('vip:agent:authDataScope')){ %>
                            actions.push('<a href="${ctx}/vip/agent/formAuthDataScope?userCode='+row.userCode+'" class="btn btn-default btn-xs btnList" title="${text('用户分配数据权限')}"><i class="fa fa-check-circle-o"></i> ${text('数据权限')}</a>&nbsp;');
                        <% } %>
                    <% if(hasPermi('vip:agent:resetpwd')){ %>
                            actions.push('<a href="${ctx}/vip/agent/resetpwd?userCode='+row.userCode+'" class="btn btn-default btn-xs btnList" title="${text('用户密码重置')}" data-confirm="${text('确认要将该用户密码重置到初始状态吗？')}"><i class="fa fa-reply-all"></i> ${text('重置密码')}</a>&nbsp;');
                        <% } %>
                        actions.push('</div>');
                    <% } %>
                    return actions.join('');
                }},
        ],
        showFooter: true,	// 是否显示底部合计行，数据载入详见
        showRownum: false,
        // 加载成功后执行事件
        ajaxSuccess: function(data){
            $('#dataGrid').dataGrid("footerData", "set", {
                "status" : "<em style='font-size: 17px'>当前页面 </em>",
                "agentBalance": "<em style='font-size: 17px'>"+countAgentBalance(data)+"</em>"
            }, false);
            $("#dataGrid").setGridParam().hideCol("roleName");//隐藏
            $("#dataGrid").setGridParam().hideCol("status");//隐藏
        }
    });
    $('#btnExport').click(function(){
        js.ajaxSubmitForm($('#searchForm'), {
            url:'${ctx}/vip/agent/exportData',
            downloadFile:true
        });
    });
    //计算代理商总余额
    function countAgentBalance(data){
        var total = 0;
        for (var i = 0; i < data.list.length ; i++) {
            var balance = data.list[i].agentBalance ;
            total += (balance == '' || balance == undefined ) ? 0.00 : balance;
        }
        return formatNumber(total);
    }
    $('#btnImport').click(function(){
        js.layer.open({
            type: 1,
            area: ['400px'],
            title: '导入用户数据',
            resize: false,
            scrollbar: true,
            content: js.template('importTpl'),
            success: function(layero, index){
                layero.find('input[type="checkbox"]').iCheck();
            },
            btn: ['<i class="fa fa-check"></i> 导入',
                '<i class="fa fa-remove"></i> 取消'],
            btn1: function(index, layero){
                var form = {
                    inputForm: layero.find('#inputForm'),
                    file: layero.find('#file').val()
                };
                if (form.file == '' || (!js.endWith(form.file, '.xls') && !js.endWith(form.file, '.xlsx'))){
                    js.showMessage("${text('文件不正确，请选择后缀为 “xls”或“xlsx”的文件。')}", null, 'warning');
                    return false;
                }
                js.ajaxSubmitForm(form.inputForm, function(data){
                    js.showMessage(data.message);
                    if(data.result == Global.TRUE){
                        js.layer.closeAll();
                    }
                    page();
                }, "json");
                return true;
            }
        });
    });
</script>
<script id="importTpl" type="text/template">//<!--
<form id="inputForm" action="${ctx}/vip/agent/importData" method="post" enctype="multipart/form-data"
	class="form-horizontal mt20 mb10" style="overflow:auto;max-height:200px;">
	<div class="row">
		<div class="col-xs-12 col-xs-offset-1">
			<input type="file" id="file" name="file" class="form-file"/>
			<div class="mt10 pt5">
				<#form:checkbox name="updateSupport" label="${text('否更新已经存在的用户数据')}" class="form-control"
					title="${text('如果用户编码已经存在，更新这条数据。')}"/> &nbsp;
				<a href="${ctx}/vip/agent/importTemplate" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> ${text('下载模板')}</a>
			</div>
			<font color="red" class="pull-left mt10">
				${text('提示：仅允许导入“xls”或“xlsx”格式文件！')}
			</font>
		</div>
	</div>
</form>
//--></script>